<?php
/**
 * 
 * @todo description of this class
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @copyright Copyright (c) 2003-2009, Nogaide BootStrap INC. All rights reserved.
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */

/**
 * @namespace
 */
namespace X;

/**
 * 
 * Generic debug formatting
 * 
 * 
 */
class Debug extends \Exception
{
    /**
     * output using CLI
     *
     * @var bool
     */
    public static $useCliOutput = false;
    /**
     * automatically print output when set to true
     *
     * @var bool
     */
    public static $Output = false;
    /**
     * quick <pre> wrapper for var_dump
     * @param <unknown> $xObject
     */
    public static  function varDump($xObject)
    {
        self::out("<pre>".var_dump($xObject)."</pre>");
    }
    /**
     * Nicely formats debug output for any variet of variables
     *
     * @staticvar string $sBGcolor
     * @staticvar string $sTGcolor
     * @param,... <unknown> $xObject
     * @return string
     */
    public static function out ($Object)
    {
        static $BGcolor = '#d4d4d4';
        $BGcolor = ($BGcolor == '#d4d4d4') ? '#c3d1df' : '#d4d4d4';
        static $TGcolor = '#d1c6de';
        $TGcolor = ($TGcolor == '#d1c6de') ? '#c7c6de' : '#d1c6de';
        

        $Backtrace = debug_backtrace();
        

        if (count($Backtrace) > 1)
        {
            $Backtrace = $Backtrace[1];
            
            $Args = array();
            foreach ($Backtrace['args'] as $Argument)
            {
                
                switch (true)
                {
                    case is_object($Argument):
                        $Args[] = get_class($Argument);
                        break;
                    case is_scalar($Argument):
                    default:
                        $Args[] = gettype($Argument);
                        break;
                }
            }
            $sArgs = implode(', ', $Args);
            if (array_key_exists('function', $Backtrace))
            {
                $Template = (self::$useCliOutput) ? '%5$s (line %6$s) <b>%1$s%2$s%3$s(%4$s)' : '%5$s (line %6$s) <b>%1$s%2$s%3$s(%4$s);</b>';
                if (array_key_exists('class' ,$Backtrace))
                {
                	$Called = sprintf($Template, @$Backtrace['class'], @$Backtrace['type'], @$Backtrace['function'], $sArgs, @$Backtrace['file'], @$Backtrace['line']);
                }
                else
                {
                	$Called = 'main()';
                }
            }
        } else
        {
            $Backtrace = array();
            $Called = 'main();';
        }
        
        if (self::$useCliOutput)
        {
            $String = "\n/*****DEBUG START*******/\n";
        }
        else
        {
            $String = "<div style='padding:2px;background-color:{$BGcolor};border: medium solid Gray'>";
            $String .= "<div style='padding:2px;background-color:{$TGcolor}'><small>debug from $Called</small></div>";
        }
        
        $Arguments = func_get_args();
        
        $Color = '#efefef';
        
        $Color = ($Color == '#efefef') ? 'White' : '#efefef';
        $sStyle = sprintf('style="padding:4px;background-color:%1$s"', $Color);
        
        if (count($Arguments) == 2 && is_string($Arguments[1]) && ! is_string($Arguments[0]))
        {
            array_unshift($Arguments, '<b>' . $Arguments[1] . '</b>');
        }
        
        foreach ($Arguments as $Argument)
        {
            $Color = ($Color == '#efefef') ? 'White' : '#efefef';
            $sStyle = sprintf('style="padding:4px;background-color:%1$s"', $Color);
            
            if (self::$useCliOutput)
            {
                switch (true)
                {
                    case (is_string($Argument)):
                        $Var = $Argument;
                        break;
                    case (get_class($Argument) == 'VARIANT'):
                        ob_start();
                        com_print_typeinfo($Argument);
                        $Var = ob_get_flush();
                        break;
                    default:
                        $Var = print_r($Argument, true);
                }
                
                $String .= sprintf('%1$s'."\n", $Var, $sStyle);
            }
            else if (is_string($Argument))
            {
                $String .= sprintf('<div %2$s>%1$s</div>', $Argument, $sStyle);
            }
            else if (is_object($Argument) && get_class($Argument) == 'VARIANT')
            {
                ob_start();
                com_print_typeinfo($Argument);
                $Var = ob_get_flush();
                $String = sprintf('<div %2$s><pre>%1$s</pre></div>', $Var, $sStyle);
            }
            else
            {
                $String .= sprintf('<div %2$s><pre>%1$s</pre></div>', htmlentities(print_r($Argument, true)), $sStyle);
            }
        }
        
        if (self::$useCliOutput)
        {
            $String .= "\n/*****DEBUG END*******/\n";
        }
        else
        {
            $String .= "</div>";
        }
        
        return $String;
    
    }
    
    public static function formatHtml ()
    {
        $Arguments = func_get_args();
        return call_user_func_array(array('X_Debug', 'out'), $Arguments);
    }
}